﻿using System.Drawing;
using System.Windows.Forms;
using monitor_2_DBwork;

namespace monitor_server_2
{
    public partial class LoginForm : Form
    {
        private readonly AccessType _access;

        public LoginForm(AccessType sender1)
        {
            InitializeComponent();
            Text = Main.Rm.GetString("UserIdentify");
            _access = sender1;
            acceptButton.TextImageRelation = TextImageRelation.ImageBeforeText;
            cancelButton.TextImageRelation = TextImageRelation.ImageBeforeText;

            //acceptButton.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            //cancelButton.AutoSizeMode = AutoSizeMode.GrowAndShrink;

            //acceptButton.AutoSize = true;
            //cancelButton.AutoSize = true;

            loginLabel.Text = Main.Rm.GetString("Login");
            passwordLabel.Text = Main.Rm.GetString("Password");

            acceptButton.Image = (Image)Main.Rm.GetObject("accept_image_16");
            cancelButton.Image = (Image)Main.Rm.GetObject("cancel_image_16");

            acceptButton.Text = Main.Rm.GetString("Accept");
            cancelButton.Text = Main.Rm.GetString("Cancel");

        }

        public enum AccessType
        {
            Admin,
            User
        }

        private void LoginForm_Shown(object sender, System.EventArgs e)
        {
            Icon = (Icon)Main.Rm.GetObject("Login_16_ico");
        }

        private void acceptButton_Click(object sender, System.EventArgs e)
        {
            var check = DBwork.CheckPassword(loginBox.Text, passwordBox.Text);
            if (check.Contains("err:"))
            {
                MessageBox.Show(check.Substring(4), Main.Rm.GetString("Error"), MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                DialogResult = DialogResult.Abort;
                return;
            }
            var access = int.Parse(check);
                bool accessGranted = true;
                switch (_access)
                {
                    case AccessType.Admin:
                        if (access > 1 || access == 0)
                        {
                            accessGranted = false;
                        }
                        break;

                    case AccessType.User:
                        if (access > 2 || access == 0)
                        {
                            accessGranted = false;
                        }
                        break;
                }
            if (accessGranted)
            {
                DialogResult = DialogResult.Yes;
            }
            else
            {
                var ret = MessageBox.Show(Main.Rm.GetString("WrongParameter") + ":\r\n" + Main.Rm.GetString("LoginPassword"),
                                Main.Rm.GetString("Error"), MessageBoxButtons.RetryCancel, MessageBoxIcon.Hand);
                if(ret == DialogResult.Cancel)
                    DialogResult = DialogResult.No;
                //DialogResult = DialogResult.No;
            }
            
        }

        private void cancelButton_Click(object sender, System.EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }
    }
}
